//
//  WaittingPayViewController.m
//  WorkNest
//
//  Created by qbm360 on 15/8/30.
//  Copyright (c) 2015年 nest. All rights reserved.
//

#import "WaittingPayViewController.h"
#import "WaittingPayCell.h"
#import "OrderPayViewController.h"
#import "PayModel.h"
#import "BulkorderModel.h"

@interface WaittingPayViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *jsonArray;

@end

static NSString *PayCellIdentifier = @"WaittingPayCell";

@implementation WaittingPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self titleView:@"待支付"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WaittingPayCell" bundle:nil]
           forCellReuseIdentifier:PayCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    [self.tableView.header beginRefreshing];
}

- (void)requestData {
    NSDictionary *dic = @{@"userName":[UserModel sharedInstance].userName, @"token":[UserModel sharedInstance].userToken, @"type":@"0"};
    [RequestData queryToPayListParameters:[UIUtil toJsonString:dic] withBlock:^(NSArray *jsonArray, NSString *msg, NSError *error) {
        if (error) {
            [self.tableView.header endRefreshing];
            if ([msg isEqualToString:@"9001"] || [msg isEqualToString:@"9002"]) {
                [UIUtil tokenOverdue:self msg:msg];
            } else {
                [[ESHUDView sharedInstance] showErrorMessage:msg==nil?@"请求失败":msg dismissBlock:nil];
            }
        } else {
            [self.tableView.header endRefreshing];
            self.jsonArray = jsonArray;
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - Table view delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.jsonArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WaittingPayCell *cell = [tableView dequeueReusableCellWithIdentifier:PayCellIdentifier];
    if (cell == nil) {
        cell = [[WaittingPayCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PayCellIdentifier];
    }
    cell.model = [self.jsonArray objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PayModel *model = [self.jsonArray objectAtIndex:indexPath.row];
    OrderPayViewController *view = [[OrderPayViewController alloc] init];
    view.toPayId = model.toPayId;
    [self.navigationController pushViewController:view animated:YES];
}

@end
